﻿using BLL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;

namespace WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class MenDianController : ControllerBase
    {
        public MenDianController(MenDian_BLL bll, IWebHostEnvironment Env)
        {
            Bll = bll;
            this.Env = Env;
        }

        public MenDian_BLL Bll { get; }
        public IWebHostEnvironment Env { get; }



        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost("TP")]
        public IActionResult TP(IFormFile file)
        {
            if (file.Length > 1024 * 1024 * 2)
            {
                return Ok(new
                {
                    data = "图片不能大于2M"
                });
            }
            if (file.ContentType != "image/jpeg")
            {
                return Ok(new
                {
                    data = "图片格式不正确"
                });
            }

            var root = Env.ContentRootPath;
            var path = root + "/img/" + file.FileName;
            using (FileStream fs = System.IO.File.Create(path))
            {
                file.CopyTo(fs);
                fs.Close();
            }
            return Ok(new
            {
                data = "https://localhost:7127" + "/img/" + file.FileName
            });


        }





        /// <summary>
        /// 门店添加
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost("MenDianAdd")]
        public IActionResult MenDianAdd(MenDian_Model m)
        {
            var m1 = Bll.MenDianAdd(m);
            return Ok(new
            {
                data = m1
            });
        }


        /// <summary>
        /// 门店列表
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="MName"></param>
        /// <param name="MPhone"></param>
        /// <param name="Count"></param>
        /// <param name="Count2"></param>
        /// <returns></returns>
        [HttpGet("MenDianShow")]
        public IActionResult MenDianShow(int index, int size, string? MName, string? MPhone)
        {
            var Count = 0;
            var Count2 = 0;
            var m = Bll.MenDianShow(index, size, MName, MPhone, out Count, out Count2);
            return Ok(new
            {
                data = m,
                count = Count,
                count2 = Count2
            });
        }
        /// <summary>
        /// 门店下拉
        /// </summary>
        /// <returns></returns>
        [HttpGet("MenDianShow2")]
        public IActionResult MenDianShow2()
        {
            var m = Bll.MenDianShow2();
            return Ok(new
            {
                data = m
            });
        }
        /// <summary>
        /// 门店删除
        /// </summary>
        /// <param name="Mid"></param>
        /// <returns></returns>
        [HttpPost("MenDianShan")]
        public IActionResult MenDianShan(int Mid)
        {
            var m = Bll.MenDianShan(Mid);
            return Ok(new
            {
                data = m
            });
        }
    }
}
